<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter title="复制销售" :width="drawerWidth" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { copyFormSchema } from '../data/data';
import { copyAdd, getEmployeeDetail } from '../data/api';
import { useAppInject } from '@apps/antd-admin/src/hooks/web/use-app-inject';

const { getIsMobile } = useAppInject();
const drawerWidth = ref('750px');
if (unref(getIsMobile)) {
  drawerWidth.value = '80%';
}
// 声明Emits
const emit = defineEmits(['success', 'register']);

const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: copyFormSchema,
});
//表单赋值
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  setDrawerProps({ confirmLoading: false });
  await resetFields();
  let record = data.record;
  let employeeId = record.employeeId;
  let result = await getEmployeeDetail({ employeeId: employeeId });
  setFieldsValue(result);
});

/**
 * 表单提交
 */
async function handleSubmit() {
  try {
    const values = await validate();
    setDrawerProps({ confirmLoading: true });
    await copyAdd(values);
    closeDrawer();
    emit('success', true);
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
